package com.jwei.web;

import com.jwei.form.CouponForm;
import com.jwei.form.GroupEdit;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Validated
public class TestController {

    @PostMapping("/save")
    public ResponseEntity save(@Validated(GroupEdit.class) CouponForm couponForm, BindingResult bindingResult) {
        System.out.println(couponForm);
        if (bindingResult.hasErrors()) {
            for (ObjectError objectError : bindingResult.getAllErrors()) {
                return new ResponseEntity(objectError.getDefaultMessage(), HttpStatus.BAD_REQUEST);
            }
        }
        return new ResponseEntity("保存成功", HttpStatus.OK);
    }

}
